/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var permute = function (nums) {
    let res = [];
    permuteLevel(nums, res, [])
    return res
};

function permuteLevel(nums, res, valArr) {
    if (valArr.length === nums.length) {
        res.push(valArr.slice(0))
        return
    }

    // 判断树枝中是否已经使用过
    let valSet = new Set(valArr)
    for (let i = 0; i < nums.length; i++) {
        if (valSet.has(nums[i])) {
            continue
        }
        valArr.push(nums[i])
        permuteLevel(nums, res, valArr)
        valArr.pop()
    }
}

// 输入：nums = [1,2,3]   数组不重复
// 输出：[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]